gg.listeninhoocode = true;
HookCode = cc.Node.extend({
	ctor:function(p){
		this._super();
		p.addChild(this, 10, TAG_CODE);
		this.init();
	},
	init : function(){
		this.setCascadeOpacityEnabled(true);
		var left_hookcode= new Button(this, 10, TAG_CODE1, "#yiqi/hookcode.png",this.callback);//砝码
		
		left_hookcode.setScale(0.7);
		left_hookcode.setPosition(400,180);
		var rotate = cc.rotateTo(0,0,180);
		left_hookcode.runAction(rotate);
		
		var right_hookcode= new Button(this, 10, TAG_CODE2, "#yiqi/hookcode.png",this.callback);
		
		right_hookcode.setScale(0.7);
		right_hookcode.setPosition(800,180);
		
		var mid_hookcode= new Button(this, 9, TAG_CODE3, "#yiqi/hookcode.png",this.callback);
		
		mid_hookcode.setScale(0.7);
		mid_hookcode.setPosition(600,180);	
	},
	addListener:function(thing){
		cc.eventManager.addListener(
				cc.EventListener.create({
					event: cc.EventListener.TOUCH_ONE_BY_ONE,
					onTouchBegan: this.onTouchBegan,
					onTouchMoved: this.onTouchMoved,
					onTouchEnded: this.onTouchEnded,
					onTouchCancelled: this.onTouchEnded
				}), thing);

	},
	onTouchBegan:function(touch, event){
		//cc.log("onTouchBegan");
		var mid_hookcode = event.getCurrentTarget();
		var pos = touch.getLocation();
		var rect = mid_hookcode.getBoundingBoxToWorld();
		if(cc.rectContainsPoint(rect, pos)&&!gg.listeninhoocode){
				mid_hookcode.canMove = true;
				mid_hookcode.setPosition(pos.x-10,pos.y+10);
				var arrow = ll.run.getChildByTag(TAG_PLASTIC).getChildByTag(TAG_PLASTIC1).getChildByTag(TAG_PLASTIC_ARROW);
				arrow.setVisible(false);
				gg.listeninpalstic = true;//塑料片不可拖动
		};
		return true;
	},
	onTouchMoved:function(touch, event){
		//cc.log("onTouchMoved");
		var mid_hookcode = event.getCurrentTarget();
		if(mid_hookcode.canMove){
			var pos = touch.getLocation();
			mid_hookcode.setPosition(pos);
		}
		
	},
	onTouchEnded:function(touch, event){
		//cc.log("onTouchEnded");
		var mid_hookcode = event.getCurrentTarget();
		var arrow = ll.run.getChildByTag(TAG_PLASTIC).getChildByTag(TAG_PLASTIC1).getChildByTag(TAG_PLASTIC_ARROW);
		var lefthookcode = ll.run.getChildByTag(TAG_CODE).getChildByTag(TAG_CODE1);
		var righthookcode = ll.run.getChildByTag(TAG_CODE).getChildByTag(TAG_CODE2);
		var pos = touch.getLocation();
		var lpos = lefthookcode.getPosition();
		var rpos = righthookcode.getPosition();
		var moveTo = cc.callFunc(function(){
			mid_hookcode.setPosition(600,180);//防止回归时左边偏移，强制回归
		},this);
		var canTouch = cc.callFunc(function(){
			gg.listeninhoocode = false;
			gg.flow.refresh();
		},this);
		var arrowFlash = cc.callFunc(function(){
			gg.listeninpalstic = false;
			arrow.setVisible(true);
		});
		var moveback = cc.moveTo(1,600,180);//回归坐标
		var leftmove = cc.moveTo(0.5,lpos.x,lpos.y-40.7); 
		var rightmove = cc.moveTo(0.5,rpos.x,rpos.y-40.7);
		var x = mid_hookcode.x;
		var y = mid_hookcode.y;
		var seq = null;
		if(mid_hookcode.canMove){
			if(x>(lpos.x-30) && x<(lpos.x+30) && y>(lpos.y-50) && y<(lpos.y)){		
				gg.listeninhoocode = true;//移动过程不可再拖动
				seq = cc.sequence(leftmove,cc.callFunc(function(){
					ll.run.plastic.plasActToLeft();//塑料片左移
				},this),cc.callFunc(function(){
					ll.run.hookcode.leftpulleyDown();//左砝码下移
					ll.run.hookcode.rightpulleyUp();//右砝码上移
					ll.run.hookcode.midpulleyLDown();//拖动砝码遂左砝码下移
					ll.run.plastic.leftlineScaleS();//塑料片左线缩短
					ll.run.plastic.rightlineScaleL();//塑料片右线伸长
					ll.run.pulley.leftlineScaleL();//左滑轮线伸长
					ll.run.pulley.rightlineScaleS();//右滑轮线缩短
				},this),cc.delayTime(1),canTouch);
			}
			else if(x>(rpos.x-30) && x<(rpos.x+30) && y>(rpos.y-50) && y<(rpos.y)){
				gg.listeninhoocode = true;
				seq = cc.sequence(rightmove,cc.callFunc(function(){
					ll.run.plastic.plasActToRight();
				},this),cc.callFunc(function(){
					ll.run.hookcode.rightpulleyDown();
					ll.run.hookcode.leftpulleyUp();
					ll.run.hookcode.midpulleyRDown();
					ll.run.plastic.leftlineScaleL();
					ll.run.plastic.rightlineScaleS();
					ll.run.pulley.rightlineScaleL();
					ll.run.pulley.leftlineScaleS();
				},this),cc.delayTime(1),canTouch);
			}
			else{
					gg.listeninhoocode = true;
					seq = cc.sequence(cc.delayTime(0.5),moveback,moveTo,canTouch,arrowFlash);//砝码回归	
				}
			mid_hookcode.runAction(seq);
		}
		mid_hookcode.canMove = false;
	},
	rightpulleyAction:function(){
		var rightpulley = this.getChildByTag(TAG_CODE2);
		var move = cc.moveTo(1,995,372.7);
		var seq = cc.sequence(move);
		rightpulley.runAction(seq);
	},
	rightpulleyUp:function(){
		var rightpulley = this.getChildByTag(TAG_CODE2);
		var move = cc.moveTo(1,995,502.7);
		var seq = cc.sequence(move);
		rightpulley.runAction(seq);
	},
	rightpulleyDown:function(){
		var rightpulley = this.getChildByTag(TAG_CODE2);
		var move = cc.moveTo(1,995,212.7);
		var seq = cc.sequence(move);
		rightpulley.runAction(seq);
	},
	leftpulleyUp:function(){
		var leftpulley = this.getChildByTag(TAG_CODE1);	
		var move = cc.moveTo(1,355,502.7);
		var seq = cc.sequence(move);
		leftpulley.runAction(seq);
	},
	leftpulleyDown:function(){
		var leftpulley = this.getChildByTag(TAG_CODE1);	
		var move = cc.moveTo(1,355,212.7);
		var seq = cc.sequence(move);
		leftpulley.runAction(seq);
	},
	midpulleyLDown:function(){
		var midpulley = this.getChildByTag(TAG_CODE3);	
		var move = cc.moveTo(1,355,172);
		var seq = cc.sequence(move);
		midpulley.runAction(seq);
	},
	midpulleyRDown:function(){
		var midpulley = this.getChildByTag(TAG_CODE3);	
		var move = cc.moveTo(1,995,172);
		var seq = cc.sequence(move);
		midpulley.runAction(seq);
	},
	callback:function(p){
		var action = gg.flow.flow.action;
		var arrow = ll.run.getChildByTag(TAG_PLASTIC).getChildByTag(TAG_PLASTIC1).getChildByTag(TAG_PLASTIC_ARROW);
		var func=cc.callFunc(function() {
			gg.flow.next();
			gg.listeninhoocode = false;
			gg.listeninpalstic = false;
			arrow.setVisible(true);
		},this);
		switch(p.getTag()){	
		case TAG_CODE1:
			var seq = cc.sequence(cc.spawn(cc.moveTo(1,cc.p(355,372.7)),
					cc.callFunc(function(){
						this.rightpulleyAction();
						var mid_hookcode = this.getChildByTag(TAG_CODE3);
						this.addListener(mid_hookcode);
						var plastic = ll.run.getChildByTag(TAG_PLASTIC).getChildByTag(TAG_PLASTIC1);
						ll.run.plastic.addListener(plastic);
					},this)),cc.delayTime(1),func);
			p.runAction(seq);
			break;
		}
	},
});